<?php
namespace app\api\controller;


use app\extra\DBCont;
use EasyWeChat\Factory;
use think\Db;

/**
 * @title 获取微信分享配置
 * @description 获取微信分享配置
 * @group 微信报名分销
 */
class WeChatShare extends Base
{
    protected $_order = 'id desc';
    protected $_noAuthorization = ['gets'];  //无需权限检测的api
    /**
     * @title 获取微信分享配置
     * @description 接口说明
     * @author hardy
     * @url /api/WeChatShare
     * @method GET
     * @param name:url type:string require:0 default: other: desc:前端分享的地址，该链接必须通过encodeURI编码后的值，并且是带有http或https开头
     * @return 和支付接口一样，直接返回微信分享的配置
     */
    public function gets()
    {
        $config = [
            'app_id' => env('WX_APP_ID'),
            'secret' => env('WX_APP_SECRET'),
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ];
        $redirect = input('get.url');
        if (empty($redirect)){
            $url = $_SERVER['REQUEST_SCHEME'].'://'.env('DOMAIN_NAME_APP');
        } else {
            $url = urldecode($redirect);
        }
        try {
            $app = Factory::officialAccount($config);
            $signPackage = $app->jssdk->setUrl($url)->buildConfig([
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                'onMenuShareQQ',
                'onMenuShareQZone',
            ]);
            exit($signPackage);
        } catch (\Exception $e){
            echo '<h1>'.$e->getMessage().'</h1>';
        }

    }
}